"""
Problem 79: https://projecteuler.net/problem=79

Passcode derivation

A common security method used for online banking is to 
ask the user for three random characters from a passcode. 
For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; 
the expected reply would be: 317.

The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, 
analyse the file so as to determine the shortest possible secret passcode of unknown length.
"""


# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/6/6
'''


Inputs = set()
with open('p079_keylog.txt', 'r') as fp:
    for row in fp:
        line = row.strip()
        for i in range(len(line)-1):
            # remove repeat item, eg. '76' in 760, 176
            # contian partical ordering
            # remove item with same digits, eg. '55'
            if line[i] != line[i+1]:
                Inputs.add(line[i]+line[i+1])
Inputs = sorted(list(Inputs))

print(Inputs)
# ['10', '12', '16', '18', '19', '20', '28', '29', '31', '36', '38', '60',
# '62', '68', '69', '71', '72', '73', '76', '79', '80', '89', '90']


def solution(data:list = Inputs):

    prioritys = dict()  # {digit:priority}

    for s in data:
        if s[0] in prioritys:
            if s[1] in prioritys:
                if prioritys[s[0]] > prioritys[s[1]]:
                    pass
                else:
                    #  conflict, creat new digit, eg. '1' --> '11'
                    prioritys[s[0]*2] = prioritys[s[1]] + 1
            else:
                prioritys[s[1]] = prioritys[s[0]] - 1
        else:
            if s[1] in prioritys:
                prioritys[s[0]] = prioritys[s[1]] + 1
            else:
                prioritys[s[0]] = 1
                prioritys[s[1]] = 0

    # print(prioritys)

    #  {'1': 1, '0': 0, '2': 0, '6': 0, '8': 0, '9': 0, '22': 1, '3': 2,
    # '66': 1, '7': 2,'77': 3, '88': 1, '99': 1}

    res_ceil = []
    for p in sorted(set(prioritys.values()), reverse=True):
        res_ceil.append({key[0] for key, val in prioritys.items() if val == p})

    print(res_ceil)
    # [{'7'}, {'7', '3'}, {'9', '1', '8', '2', '6'}, {'9', '8', '0', '2', '6'}]



    tmp = []
    for i in range(len(res_ceil)-1):
        comm = res_ceil[i] & res_ceil[i+1]
        tmp.append(comm)
        res_ceil[i]= res_ceil[i] -  comm
        res_ceil[i+1] = res_ceil[i+1] - comm

    res = []
    #  insert {comm}
    for i in range(len(res_ceil)-1):
          res.append(res_ceil[i])
          res.append(tmp[i])
    res.append(res_ceil[-1])

    res = [s for s in res if s]
    print(res)
    # [set(), {'7'}, {'3'}, set(), {'1'}, {'2', '6', '9', '8'}, {'0'}]
    # [{'7'}, {'3'}, {'1'}, {'6', '2', '8', '9'}, {'0'}]


    # [UNDO] {'2', '6', '9', '8'} ?order
    for i in range(len(res)):
        if len(res[i]) >1:
            dataSub = [s for s in data if s[0] in res[i] and s[1] in res[i]]
            res[i] = solution(dataSub)

    return res


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(''.join([c for x in solution() for y in x for c in y]))
    # 73162890
